home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / vbpxen.zip / VVDBMOD.BAS < prev    next >
BASIC Source File  |  1991-10-07  |  8KB  |  236 lines

  1.  
  2. '
  3. '   Written by Steve Jackson
  4. '              9152 Brabham Dr.
  5. '              Huntington Beach, CA 92646
  6. '
  7. '   This is meant to be called from your form objects.  In turn, these
  8. '   functions call routines in PXMODULE.BAS that access Paradox.  I
  9. '   tried to isolate all Paradox specific code there in case you want
  10. '   to change your app to some other DBMS later (SQL Server, xbase, etc.)
  11. '   or it you don't like it and want to change it...
  12. '
  13. Function StartUp () As Integer
  14.     '
  15.     '  Initialize the database system, with a user id
  16.     '  open all tables
  17.     '
  18.     rc = DBInit("vvdemo")
  19.     '
  20.     '  If table open fails, pxerror() routine displays a message,
  21.     '  then we shut down
  22.     '
  23.     '  If you create an EXE for this program,
  24.     '  you can code this to get directory where
  25.     '  the program is.  Otherwise use the global constant
  26.     '  because at development time CurDir$ tells you where
  27.     '  Visual Basic is, not your project.
  28.     '
  29.     '  ***** db_dir$ = CurDir$  *****
  30.     '
  31.     db_dir$ = DEFAULT_DB_DIR
  32.     '
  33.     tbl_name$ = db_dir$ + "\customer"
  34.     rc = TableOpen(CUSTOMER_TABLE, tbl_name$)
  35.     If rc Then
  36.         rc = DBExit()
  37.         End
  38.     End If
  39.  
  40.     tbl_name$ = db_dir$ + "\item"
  41.     rc = TableOpen(ITEM_TABLE, tbl_name$)
  42.     If rc Then
  43.         rc = DBExit()
  44.         End
  45.     End If
  46.  
  47.     StartUp = DB_OK
  48. End Function
  49.  
  50. Function Shutdown () As Integer
  51.     '
  52.     '  Terminate the database system, close tables
  53.     '  this is invoked at program end time
  54.     '
  55.     rc = DBExit()
  56.     Shutdown = rc
  57. End Function
  58.  
  59. Function GetCustomerRec (ByVal Action%) As Integer
  60.     '
  61.     '   Get the customer record and move all fields to
  62.     '   a record buffer that is global
  63.     '
  64.     If Action% = DBKEYED Then
  65.         rc = PutAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  66.     End If
  67.  
  68.     rc = GetRec(CUSTOMER_TABLE, Action%)
  69.     '
  70.     '  Assume the error handling function traps fatal errors and
  71.     '  ends the program.  Here we assume any error is of the expected
  72.     '  variety, such as not-found, end-of-file, duplicate-key, etc.
  73.     '
  74.     If rc = DB_NOTFOUND Then
  75.         GetCustomerRec = rc
  76.         Beep
  77.         Msg$ = "Customer not found for this customer number"
  78.         MsgBox Msg$, MB_ICONINFORMATION, "Get Customer"
  79.         Exit Function
  80.     End If
  81.     '
  82.     '  Assume that if there is still and error, it is at end or
  83.     '  start of file.  Just beep, but do not display any msg
  84.     '
  85.     If rc Then
  86.         GetCustomerRec = rc
  87.         Beep
  88.         Exit Function
  89.     End If
  90.     '
  91.     '  Move fields from paradox to the record buffer
  92.     '  The fields are NOT on the form at this point
  93.     '
  94.     rc = GetAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  95.     rc = GetAlphaField(CUSTOMER_TABLE, 2, custrec.lastname)
  96.     rc = GetAlphaField(CUSTOMER_TABLE, 3, custrec.firstname)
  97.     rc = GetAlphaField(CUSTOMER_TABLE, 4, custrec.address)
  98.     rc = GetAlphaField(CUSTOMER_TABLE, 5, custrec.city)
  99.     rc = GetAlphaField(CUSTOMER_TABLE, 6, custrec.zip)
  100.     rc = GetShortField(CUSTOMER_TABLE, 7, custrec.tapes_out)
  101.     rc = GetNumField(CUSTOMER_TABLE, 8, custrec.total_spent)
  102.  
  103.     GetCustomerRec = DB_OK
  104. End Function
  105.  
  106. Function UpdateCustomerRec () As Integer
  107.     '
  108.     '  Write the current record back to the database.
  109.     '  Assume no-one else has changed the positioning since
  110.     '  the time we got the record, and when the update takes place.
  111.     '  Note:  this may be a dangerous assumption in Windows...
  112.     '
  113.     rc = PutAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  114.     rc = PutAlphaField(CUSTOMER_TABLE, 2, custrec.lastname)
  115.     rc = PutAlphaField(CUSTOMER_TABLE, 3, custrec.firstname)
  116.     rc = PutAlphaField(CUSTOMER_TABLE, 4, custrec.address)
  117.     rc = PutAlphaField(CUSTOMER_TABLE, 5, custrec.city)
  118.     rc = PutAlphaField(CUSTOMER_TABLE, 6, custrec.zip)
  119.     rc = PutShortField(CUSTOMER_TABLE, 7, custrec.tapes_out)
  120.     rc = PutNumField(CUSTOMER_TABLE, 8, custrec.total_spent)
  121.  
  122.     rc = UpdateRec(CUSTOMER_TABLE)
  123.     UpdateCustomerRec = rc
  124.  
  125.     If rc Then
  126.         Beep
  127.         Msg$ = "Update failed, reason code: " + Str$(rc)
  128.         MsgBox Msg$, MB_ICONEXCLAMATION, "Update Customer"
  129.     End If
  130.  
  131.     rc = UnlockRec(CUSTOMER_TABLE)
  132. End Function
  133.  
  134. Function AddCustomerRec () As Integer
  135.     '
  136.     '  Write the record to the database.
  137.     '  Assume no-one else has already added one with this key.
  138.     '  Note:  this may be a dangerous assumption in Windows...
  139.     '
  140.     rc = PutAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  141.     rc = PutAlphaField(CUSTOMER_TABLE, 2, custrec.lastname)
  142.     rc = PutAlphaField(CUSTOMER_TABLE, 3, custrec.firstname)
  143.     rc = PutAlphaField(CUSTOMER_TABLE, 4, custrec.address)
  144.     rc = PutAlphaField(CUSTOMER_TABLE, 5, custrec.city)
  145.     rc = PutAlphaField(CUSTOMER_TABLE, 6, custrec.zip)
  146.     rc = PutShortField(CUSTOMER_TABLE, 7, custrec.tapes_out)
  147.     rc = PutNumField(CUSTOMER_TABLE, 8, custrec.total_spent)
  148.  
  149.     rc = AddRec(CUSTOMER_TABLE)
  150.     AddCustomerRec = rc
  151.     '
  152.     '  assume serious errors were trapped in pxerror()
  153.     '  if the add fails, assume it is a duplicate key
  154.     '
  155.     If rc Then
  156.         Beep
  157.         Msg$ = "ADD failed - there is already a customer with this number"
  158.         MsgBox Msg$, MB_ICONINFORMATION, "Add Customer"
  159.     End If
  160.  
  161.     AddCustomerRec = rc
  162. End Function
  163.  
  164. Function DeleteCustomerRec () As Integer
  165.     '
  166.     '  Write the current record back to the database.
  167.     '  Assume no-one else has changed the positioning since
  168.     '  the time we got the record, and when the update takes place.
  169.     '  Note:  this may be a dangerous assumption in Windows...
  170.     '
  171.     '  Just move the key field to the record buffer
  172.     '
  173.     rc = PutAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  174.  
  175.     rc = DeleteRec(CUSTOMER_TABLE)
  176.     '
  177.     '  assume serious errors were trapped in pxerror()
  178.     '  if the delete fails, assume it was already deleted
  179.     '
  180.     If rc Then
  181.         Beep
  182.         Msg$ = "DELETE failed - Customer was already deleted"
  183.         MsgBox Msg$, MB_ICONEXLAMATION, "Delete Customer"
  184.     End If
  185.  
  186.     DeleteCustomerRec = rc
  187. End Function
  188.  
  189. Function GetCustomerRecForUpdate () As Integer
  190.     '
  191.     '   Get the customer record by key value,
  192.     '   and place a record lock on it.
  193.     '
  194.     '   Move all fields to a record buffer that is global
  195.     '
  196.     rc = PutAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  197.     
  198.     rc = GetRec(CUSTOMER_TABLE, DB_KEYED)
  199.     '
  200.     '  Assume the error handling function traps fatal errors and
  201.     '  ends the program.  Here we assume any error is of the expected
  202.     '  variety, such as not-found, end-of-file, duplicate-key, etc.
  203.     '
  204.     If rc Then
  205.         GetCustomerRecForUpdate = rc
  206.         Beep
  207.         Msg$ = "Customer record was not found for this customer number"
  208.         MsgBox Msg$, MB_ICONINFORMATION, "Get Customer"
  209.         Exit Function
  210.     End If
  211.     '
  212.     '  Place the lock,
  213.     '    if it fails, try again until user quits
  214.     '
  215.     rc = LockRec(CUSTOMER_TABLE)
  216.     If rc Then
  217.         GetCustomerRecForUpdate = rc
  218.         Msg$ = "Customer record is locked by someone else"
  219.         MsgBox Msg$, MB_ICONINFORMATION, "Get Customer"
  220.         Exit Function
  221.     End If
  222.  
  223.  
  224.     rc = GetAlphaField(CUSTOMER_TABLE, 1, custrec.custnumber)
  225.     rc = GetAlphaField(CUSTOMER_TABLE, 2, custrec.lastname)
  226.     rc = GetAlphaField(CUSTOMER_TABLE, 3, custrec.firstname)
  227.     rc = GetAlphaField(CUSTOMER_TABLE, 4, custrec.address)
  228.     rc = GetAlphaField(CUSTOMER_TABLE, 5, custrec.city)
  229.     rc = GetAlphaField(CUSTOMER_TABLE, 6, custrec.zip)
  230.     rc = GetShortField(CUSTOMER_TABLE, 7, custrec.tapes_out)
  231.     rc = GetNumField(CUSTOMER_TABLE, 8, custrec.total_spent)
  232.  
  233.     GetCustomerRecForUpdate = DB_OK
  234. End Function
  235.  
  236.